package com.gouwudai.gobbs.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IFriendsDAO;
import com.gouwudai.gobbs.pojo.TbbsFriends;
import com.litt.core.common.PageParam;
import com.litt.core.common.Utility;
/**
 * 
 * <pre>
 *  Title:我的好友DAO
 *  Description: TODO  
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-3-29
 * @version 1.0
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容: 
 * </pre>
 */
public class FriendsDAOImpl extends BaseDAOImpl implements IFriendsDAO 
{
	private static final Logger logger = Logger.getLogger(FriendsDAOImpl.class);	
	private static final String BUSI_NAME = "我的好友";
	
	/**
	 * @param tbbsFriends
	 */
	public void save(TbbsFriends tbbsFriends)
	{    		
		super.saveObject(tbbsFriends);		
	}
	
	/**
	 * @param id
	 * @return
	 */
	public TbbsFriends load(Integer id)
	{
		return (TbbsFriends)super.loadObject(TbbsFriends.class, id);
	} 
	
	/**
	 * @param friIds
	 */
    public void doAcceptFriends(String friIds)
    {
    	StringBuilder hql = new StringBuilder();
        hql.append("update TbbsFriends set addFlag=1,addDatetime=?");         
        hql.append(" where friId in(");
        hql.append(friIds);
        hql.append(")");
        super.updateObject(hql.toString(), new Object[]{new Date()});
    }	
    
	/**
	 * @param friIds
	 */
    public void doBlackFriends(String friIds)
    {
    	StringBuilder hql = new StringBuilder();
        hql.append("update TbbsFriends set addFlag=1,addDatetime=?,isBlack=1");         
        hql.append(" where friId in(");
        hql.append(friIds);
        hql.append(")");
        super.updateObject(hql.toString(), new Object[]{new Date()});
    }    
	
	/**
	 * @param friId
	 * @param isBlack
	 */
    public void updateBlack(Integer friId,Boolean isBlack)
    {
    	StringBuilder hql = new StringBuilder();
        hql.append("update TbbsFriends set isBlack=?");         
        hql.append(" where friId=?");
        super.updateObject(hql.toString(), new Object[]{isBlack,friId});
    }
    
	/**
	 * @param friendLoginId
	 * @param isOnline
	 */
    public void updateOnline(String friendLoginId,Boolean isOnline)
    {
    	StringBuilder hql = new StringBuilder();
        hql.append("update TbbsFriends set isOnline=?");         
        hql.append(" where friendLoginId=?");
        super.updateObject(hql.toString(), new Object[]{isOnline,friendLoginId});
    }    
    
    /**
	 * @param friIds
	 */
    public void delete(String friIds)
    {
        super.deleteObjects(TbbsFriends.class.getName(),"friId",friIds);
    }
    
    /**
	 * @param userLoginId
	 * @return
	 */
    public List listAllFriends(String userLoginId)
    {
    	String hql = "from TbbsFriends obj where obj.userLoginId='"+userLoginId+"'";
    	return super.listAll(hql);    	
    }
    
    /**
	 * @param userLoginId
	 * @return
	 */
    public List filterFriends(String userLoginId)
    {
    	String hql = "select obj.friendLoginId from TbbsFriends obj where obj.userLoginId='"+userLoginId+"')";
    	return super.listAll(hql);     	
    }   
    
	/**
	 * @return
	 */
    public List listAllActiveLoginId()
    { 
    	String hql = "select loginId from TbbsUserInfo where isdelFlag=0";
    	return super.listAll(hql);
    }     

    /**
     * 分页查询
     * 
     * @param pageParam 分页查询对象
     * 
     * @return
     */
    public IPageList listPage(PageParam pageParam)
    {   
        String loginId = pageParam.getStringCond("loginId");
        
        int friendType = Utility.parseInt(pageParam.getIntegerCond("friendType"));
        String cond = "AND A.ADD_FLAG=1 AND A.IS_BLACK=0 AND";//好友
    	if(friendType==1) //准好友
    	{
    		cond = "AND A.ADD_FLAG=0 AND A.IS_BLACK=0 AND";
    	}
    	else if(friendType==2) //黑名单
    	{
    		cond = "AND A.ADD_FLAG=1 AND A.IS_BLACK=1 AND";
    	}   
    	StringBuilder listSql = new StringBuilder("SELECT A.*,B.SEX,B.USER_NAME,B.REG_TIME,B.LAST_LOGIN_TIME,C.AREA,C.CITY FROM TbbsFriends A,TbbsUserInfo B,TbbsUserDetail C WHERE A.USER_LOGIN_ID='"+loginId+"' "+cond+" A.FRIEND_LOGIN_ID=B.LOGIN_ID AND B.USER_ID=C.USER_ID ");
    	if(!Utility.isEmpty(pageParam.getSort()))
    	    listSql.append(pageParam.getSort());
    	return super.listSqlPage(listSql.toString(),pageParam.getPageIndex(),pageParam.getPageSize());
    }
    
    /**
	 * @param loginId
	 * @param friendType
	 * @return
	 */
    public int listCount(String loginId,int friendType)
    {
    	String cond = "";
    	if(friendType==1) //准好友
    	{
    		cond = "AND A.ADD_FLAG=0 AND A.IS_BLACK=0 AND";
    	}
    	else if(friendType==2) //黑名单
    	{
    		cond = "AND A.ADD_FLAG=1 AND A.IS_BLACK=1 AND";
    	}
    	else	//好友
    	{
    		cond = "AND A.ADD_FLAG=1 AND A.IS_BLACK=0 AND";
    	}
    	StringBuilder listSql = new StringBuilder("SELECT COUNT(*) FROM TbbsFriends A,TbbsUserInfo B,TbbsUserDetail C WHERE A.USER_LOGIN_ID='"+loginId+"' "+cond+" A.FRIEND_LOGIN_ID=B.LOGIN_ID AND B.USER_ID=C.USER_ID");
    	return super.listSqlCount(listSql.toString());
    }
    
}
